package com.fengxing.ams.web.facade;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.fengxing.ams.pub.persistence.DynamicSpecifications;
import com.fengxing.ams.pub.persistence.SearchFilter;
import com.fengxing.ams.pub.persistence.SearchFilter.Operator;
import com.fengxing.ams.web.action.view.CollegeQueryVO;
import com.fengxing.ams.web.facade.intf.ICollegeFacade;
import com.fengxing.ams.web.facade.mouble.college.CollegeVO;
import com.fengxing.ams.web.facade.mouble.college.ICollegeDAO;

@Component
@Transactional
public class CollegeFacade implements ICollegeFacade{

	@Autowired
	ICollegeDAO collegeDAO;

	@Override
	public CollegeVO getCollegeInfo(Integer collegeId) {
		return collegeDAO.findOne(collegeId);
	}

	@Override
	public List<CollegeVO> getCollegeAll() {
		List<CollegeVO> result = new ArrayList<CollegeVO> ();
		Sort sort = new Sort(Sort.Direction.ASC, "otherOrder");
		Iterator<CollegeVO> iter = collegeDAO.findAll(sort).iterator();
		while (iter.hasNext()) {
			result.add(iter.next());
		}
		return result;
	}

	@Override
	public Page<CollegeVO> queryCollegeBaseList(CollegeQueryVO queryVO) {
		//进入默认查询
		if (queryVO == null) {
			Sort sort = new Sort(Sort.Direction.ASC, "zytOrder");
			PageRequest pr = new PageRequest(0, 20, sort );
			Page<CollegeVO> page = collegeDAO.findAll(pr);
			return page;
		}
		List<SearchFilter> searchFilterList = new ArrayList<SearchFilter>();
		SearchFilter filter = null;
		
		String principle = queryVO.getPrinciple();
		if (StringUtils.isNotBlank(principle)) {
			filter  = new SearchFilter("principle", Operator.EQ, principle, null);
			searchFilterList.add(filter);
		}
		
		String type = queryVO.getType();
		if (StringUtils.isNotBlank(type)) {
			filter  = new SearchFilter("type", Operator.EQ, type, null);
			searchFilterList.add(filter);
		}
		
		String q211 = queryVO.getQ211();
		if (StringUtils.isNotBlank(q211) && q211.equals("on")) {
			filter  = new SearchFilter("qualification_211", Operator.EQ, 1, null);
			searchFilterList.add(filter);
		}
		String q985 = queryVO.getQ985();
		if (StringUtils.isNotBlank(q985) && q985.equals("on")) {
			filter  = new SearchFilter("qualification_985", Operator.EQ, 1, null);
			searchFilterList.add(filter);
		}
		String qg = queryVO.getQg();
		if (StringUtils.isNotBlank(qg) && qg.equals("on")) {
			filter  = new SearchFilter("qualification_graduate", Operator.EQ, 1, null);
			searchFilterList.add(filter);
		}
		
		String location = queryVO.getLocation();
		if (StringUtils.isNotBlank(location)) {
			String[] splits = location.split(",");
			filter  = new SearchFilter("location", Operator.IN, splits);
			searchFilterList.add(filter);
		}
		
		String edu = queryVO.getEdu();
		if (StringUtils.isNotBlank(edu)) {
			if (edu.equals("本科")) {
				filter  = new SearchFilter("education_level", Operator.NEQ, "高职(专科)", null);
				searchFilterList.add(filter);
			} else if (edu.equals("专科")) {
				filter  = new SearchFilter("education_level", Operator.NEQ, "本科", null);
				searchFilterList.add(filter);
			}
		}
		
		Sort sort = new Sort(Sort.Direction.ASC, "zytOrder");
		Pageable pr = null;
		
		Integer order = queryVO.getOrder();
		if (order == 2) {
			sort = new Sort(Sort.Direction.ASC, "otherOrder");
		}
		
		Integer page = queryVO.getPage();
		if (page != null)
			pr = new PageRequest(page, 20, sort);
		
		Specification<CollegeVO> spec = DynamicSpecifications.bySearchFilter(
				searchFilterList, CollegeVO.class);
		return this.collegeDAO.findAll(spec, pr);
	}
	
}
